#pragma once 
#include<iostream>
#include<string>
#include<sstream>
#include<sys/types.h>
#include<sys/socket.h>
#include<unordered_map>

class Util{

  public:
    static int ReadLine(int sock,std::string &line){
       char ch='x';
       while(ch!='\n'){
          ssize_t s=recv(sock,&ch,1,0);
          if(s>0){
            if(ch=='\r'){
              recv(sock,&ch,1,MSG_PEEK);
              if(ch=='\n'){
                recv(sock,&ch,1,0);
              }
              else{
                ch='\n';
              }
            }
            line.push_back(ch);
          }
          else if(s==0){
            return 0;
          }
          else{
            return -1;
          }
       }
       return line.size();
    }
  

    static void OneLine(std::string &request_line,std::string &a,std::string &b,std::string &c)
    {
      std::stringstream ss(request_line);
      ss>>a>>b>>c;
    }

    static void TowLine(std::string &request_headline,std::string &k,std::string &v,std::string tp)
    {
      size_t pos=request_headline.find(tp);
      if(pos!=std::string::npos){
          k=request_headline.substr(0,pos);
          v=request_headline.substr(pos+tp.size());
      }
    }
};

